//打折促销
package shopping

import (
    //"fmt"
    "time"
	"errors"
	"strings"
	//"strconv"
	"dhfshop/models"
	"dhfshop/models/consts"
	"github.com/astaxie/beego/orm"
)

// 活动页面表, 存储各种商家自定义或者咱们系统生成的 与 促销活动相关的展示页面。
type ActivityPage struct {
    ActivityPageId int64     `orm:"column(activity_page_id);pk;auto" json:"activity_page_id" form:"activity_page_id"` //活动页面ID
    PartnerId      int64     `orm:"column(partner_id)" json:"partner_id" form:"partner_id"`         //商家ID
	StoreId        int64     `orm:"column(store_id)" json:"store_id" form:"store_id"`               //门店ID
	AppId          string    `orm:"column(app_id);size(32);null" json:"app_id" form:"app_id"`       //小程序id，默认为空
	Title          string    `orm:"column(title);size(256);null" json:"title" form:"title"`         //标题
	Img            string    `orm:"column(img);size(256);null" json:"img" form:"img"`               /* */
	Content        string    `orm:"column(content);type(text);null" json:"content" form:"content"`  //用于存储页面json字符串
	Types          string    `orm:"column(types);size(24)" json:"types" form:"types"`               //类型
	Sys            int8      `orm:"column(sys)" json:"sys" form:"sys"`                              //1 商家创建  2  系统创建商家只读
	Aid            int64     `orm:"column(aid);default(0)" json:"aid" form:"aid"`                   //动相关的活动主键id
	ItemId         string    `orm:"column(item_id);size(256);null" json:"item_id" form:"item_id"`   //商品id,逗号分割多个
	CatId          string    `orm:"column(cat_id);size(256);null" json:"cat_id" form:"cat_id"`      //类目id,逗号分割多个
	Visible        int8      `orm:"column(visible);default(1)" json:"visible" form:"visible"`       //是否可见,默认=1可见2=不可见
	CreatedAt      time.Time   `orm:"auto_now_add;column(created_at);type(datetime)" json:"-" form:"-"` 
	CreatedAtStr   string      `orm:"-" json:"created_at" form:"-"`
    UpdatedAt      time.Time   `orm:"auto_now;column(updated_at);type(datetime)" json:"-" form:"-"`         
	UpdatedAtStr   string      `orm:"-" json:"updated_at" form:"-"`
	Note           string      `orm:"column(note);size(256);null" json:"note" form:"note"`         //备注 
}

func init() {
    orm.RegisterModel(new(ActivityPage))   
}

func AddActivityPage(m *ActivityPage) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}

func GetActivityPageById(id int64) (v *ActivityPage, err error) {
	o := models.GetDefaultDB()
	v = &ActivityPage{ActivityPageId: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}


func GetAllActivityPages(query map[string]string,fields []string,order_by map[string]string,offset,limit int)([]*ActivityPage,int64,error) {
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(ActivityPage))
	for k, v := range query {
		k = strings.Replace(k, ".", "__", -1)
		qs = qs.Filter(k, v)
	}
	cnt, err := qs.Count()
	// order by:
	var sortFields []string
	for k, v := range order_by {
		orderby := ""
		if v == "desc" {
			orderby = "-" + k
		} else if v == "asc" {
			orderby = k
		} else {
			return nil, 0, errors.New("Error: Invalid order. Must be either [asc|desc]")
		}
		sortFields = append(sortFields, orderby)
	}
	qs = qs.OrderBy(sortFields...)
	var lists []*ActivityPage
	if _, err := qs.Limit(limit, offset).All(&lists, fields...); err == nil {
	    for _, temp := range lists {
			temp.CreatedAtStr = temp.CreatedAt.Format(consts.DATETIME_FORMAT)
			temp.UpdatedAtStr = temp.UpdatedAt.Format(consts.DATETIME_FORMAT)
	    }
		return lists, cnt, nil
	}
	return nil, 0, err
}

func UpdateActivityPageById(m *ActivityPage, k ...string) (err error) {
	o := models.GetDefaultDB()
	v := ActivityPage{ActivityPageId: m.ActivityPageId}
	if err = o.Read(&v); err != nil {
		return err
	}else{
	    if _, err = o.Update(m, k...); err != nil {
		   return err
		}
	}
	return nil
}

func DeleteActivityPage(id int64) (err error) {
	o := models.GetDefaultDB()
	v := ActivityPage{ActivityPageId: id}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return nil
		}
	}
	return
}

